# import numpy as np
import pandas as pd
from sklearn.model_selection import StratifiedKFold
# import lightgbm as lgbm
# import _pickle as pickle
import time
from lightgbm.sklearn import LGBMClassifier
from sklearn.model_selection import GridSearchCV
from scipy.sparse import csr_matrix

import warnings
warnings.filterwarnings("ignore")

begin_time = time.time()


def timer(s=0):
    global begin_time
    if s == 1:
        begin_time = time.time()
    else:
        print(time.time() - begin_time)


X_train = pd.read_pickle("pkl/X_train")
X_train = X_train.sample(n=2000000, random_state=0, axis=0)
print("X_train read")
X_train = csr_matrix(X_train)
print("CSR")
y_train = pd.read_pickle("pkl/y_train")
y_train = y_train.sample(n=2000000, random_state=0, axis=0)

MAX_ROUNDS = 10000
kfold = StratifiedKFold(n_splits=3, shuffle=True, random_state=3)

params = {'boosting_type': 'gbdt',
          'objective': 'binary',
          'n_jobs': -1,
          'learning_rate': 0.1,
          'n_estimators': 863,
          'max_depth': 7,
          'max_bin': 127,  # 2^6,原始特征为整数，很少超过100
          'subsample': 0.7,
          'bagging_freq': 1,
          'colsample_bytree': 0.7,
          'verbose=': -1
          }
lg = LGBMClassifier(silent=False, **params)
num_leaves_s = range(62, 67, 1)
tuned_parameters = dict(num_leaves=num_leaves_s)
timer(1)
grid_search1 = GridSearchCV(lg, n_jobs=-1, param_grid=tuned_parameters, cv=kfold, scoring="neg_log_loss", verbose=-1,
                            refit=False)
grid_search1.fit(X_train, y_train)
timer()
# In[ ]:


# sklearn.metrics.SCORERS.keys()


# In[ ]:


print(-grid_search1.best_score_)
print(grid_search1.best_params_)

fo = open("res.txt", "a+")
fo.write("num_leaves: " + str(grid_search1.best_params_) + "\n")
fo.close()
